const db = require("../models");
const Tech = db.tech;
const Learning_route = db.learning_route;

//每日打卡，增加该用户某个技术的进度
exports.dailyCheck = async (req, res) => {
  try {
    var nowProcess = new Number();
    var checkProcess = 0.1;
    var tid;
    await Tech.findOne({
      where: {
        tech_name: req.query.tech_name,
      },
      raw: true,
    }).then((data) => {
      tid = data.tech_id;
    });
    await Learning_route.findOne({
      where: { user_mail: req.query.user_mail, tech_id: tid },
      raw: true,
    }).then((data) => {
      console.log(data);
      nowProcess = data.process;
    });
    if (req.query.process != null) {
      nowProcess += Number(req.query.process);
    } else {
      nowProcess += checkProcess;
    }

    await Learning_route.update(
      { process: nowProcess },
      {
        where: { user_mail: req.query.user_mail, tech_id: tid },
      }
    );
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.send("check success!");
  } catch (err) {
    res.status(500).send({
      message: err.message || "学习路线中不存在该技术",
    });
  }
};
